// pages/withdraw/withdraw.js

var app = getApp();
var page=1;
var selectedNews = false;
var newsList = [];
var haveTap = 1; // 1: 可以点击 0：不可点击

Page({
  data: {
    
    listdefault: 1,
    nocard: false,
    cardlist: {},
    cardId: "",
    allMoney: "",
    money: "",
    withdraw_money: "",
    withdraw_rate1: "",
    withdraw_rate: "",
    withdraw_min: "",
    changeMoney: false,
    pay_money: "",
    tishiDialogShow:false,
    tishiDialogContent:""
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    that.setData({
      allMoney: options.allMoney
    })
    // var url = '/wallet_api/bankList';
    // var params = {
    //   uid: app.globalData.userInfo.id
    // };
    // app.HttpPostSend(url, params, function (res) {
    //   if (res.msg != "success") {
    //     app.showTipMsg(res.msg);
    //   } else {
    //     var cardlist = res.data;
    //     if (res.data) {
    //       if (cardlist.length > 0) {
    //         var cards = cardlist[0];
    //         cards.card_number = cards.card_number.slice(0,4);
    //         var cardId = cardlist[0].id;

    //         if (!!options.info) {
    //           cards = JSON.parse(options.info);
    //           cards.card_number = cards.card_number.slice(0, 4);
    //           cardId = cards.id;
    //         }
    //         that.setData({
    //           cardlist: cards,
    //           cardId: cardId,
    //           nocard: false
    //         })
    //       } else {
    //         that.setData({
    //           cardId: cardId,
    //           nocard: true
    //         })
    //       }
    //     } else {
    //       that.setData({
    //         cardId: '',
    //         nocard: true
    //       })
    //     }
    //   }
    // });

    var url2 = '/wallet_api/withdrawal';
    var params2 = {};
    app.HttpPostSend(url2, params2, function (res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        if (res.data) {
          that.setData({
            withdraw_money: res.data.money,
            withdraw_rate1: res.data.rate,
            withdraw_rate: res.data.rate/100,
            withdraw_min: res.data.min,
          })
        }
      }
    });
  },

  // 打开银行卡 页面
  showMyBankCard: function () {
    if (this.data.nocard == true){ 
      wx.navigateTo({
        url: '/pages/bankCard/bankCard'
      })
    } else {
      wx.navigateTo({
        url: '/pages/myBankCard/myBankCard?backtype=withdraw'
      })
    }
  },

  inputMoney: function (e) {
    if (e.detail.value == ""){
      this.setData({
        money: e.detail.value,
        changeMoney: false
      });
    }else{
      var paymoney = Number(e.detail.value) * this.data.withdraw_rate;
      if (paymoney<this.data.withdraw_min){
        paymoney = this.data.withdraw_min;
      }
      this.setData({
        money: e.detail.value,
        changeMoney: true,
        pay_money: paymoney
      });
    }
  },

  //全部提现
  withdrawAll:function(){
    this.setData({
      money: this.data.allMoney,
      changeMoney: true,
      pay_money: Number(this.data.allMoney) * this.data.withdraw_rate1
    });
  },
  //提现到银行卡
  showWithdraw: function () {
    var that = this;
    // if (that.data.cardId == ''){
    //   app.showTipMsg('请选择银行卡！');
    // } else 

    if (that.data.money == '') {
      that.setData({
        tishiDialogShow: true,
        tishiDialogContent: '请输入金额！'
      })
      setTimeout(function(){
        that.setData({
          tishiDialogShow: false,
          tishiDialogContent: ''
        })
      }, 2000)
    } else {
      if (Number(that.data.money) > Number(that.data.allMoney)) {
        that.setData({
          tishiDialogShow: true,
          tishiDialogContent: '账户余额不足！'
        })
        setTimeout(function () {
          that.setData({
            tishiDialogShow: false,
            tishiDialogContent: ''
          })
        }, 2000)
        return;
      }
      if (Number(that.data.money) < that.data.balance) {
        that.setData({
          tishiDialogShow: true,
          tishiDialogContent: '金额不能小于' + that.data.balance +'！'
        })
        setTimeout(function () {
          that.setData({
            tishiDialogShow: false,
            tishiDialogContent: ''
          })
        }, 2000)
        return;
      }
      if (!(/(^[1-9]\d*$)/.test(Number(that.data.money)))) {
        that.setData({
          tishiDialogShow: true,
          tishiDialogContent: '请输入大于0整数金额！'
        })
        setTimeout(function () {
          that.setData({
            tishiDialogShow: false,
            tishiDialogContent: ''
          })
        }, 2000)
        return;
      }
      if (haveTap == 1) {
        wx.showModal({
          title: '提示',
          content: '确定要提现吗?',
          success(res) {
            if (res.confirm) {
              haveTap = 0;
              var url2 = '/wallet_api/addCash_v4';
              var params2 = {
                uid: app.globalData.userInfo.id,
                // card_id: that.data.cardId,
                money: that.data.money,
                timestamp: new Date().getTime(),
              };
              params2.sign = app.getSign(params2)
              app.HttpPostSend(url2, params2, function (res) {
                if (res.code != "1") {

                  that.setData({
                    tishiDialogShow: true,
                    tishiDialogContent: res.data
                  })
                  setTimeout(function () {
                    that.setData({
                      tishiDialogShow: false,
                      tishiDialogContent: ''
                    })
                  }, 2000)
                  haveTap = 1;
                  // app.showTipMsg(res.data);
                } else {

                  // 发送模板消息-提现申请通知
                  // app.sendTemplateMessage(app.globalData.SwitchTabParams.v_id, 7, '/radarPages/radarCenter/radarCenter', '微信钱包', '', that.data.money, that.data.withdraw_rate1)
                  that.setData({
                    tishiDialogShow: true,
                    tishiDialogContent: '申请提现成功！'
                  })
                  setTimeout(function () {
                    that.setData({
                      tishiDialogShow: false,
                      tishiDialogContent: ''
                    })
                    wx.navigateBack({
                      url: '/pages/myMoney/myMoney',
                    })
                  }, 2000)
                  haveTap = 1;
                }
              });
            } else if (res.cancel) {
              haveTap = 1;
              console.log('用户点击取消')
            }
          }
        })
      }
    }
  },

  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示

  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  // onShareAppMessage: function () {
  //   return {
  //     title: '万商',
  //     desc: '你要的名片代',
  //     path: '/pages/home/home'
  //   }
  // },
  /** 
   * 页面上拉触底事件的处理函数 
   */
  onReachBottom: function () {
  }
})